const app = getApp()
import { saveOrUpdateShippingAddr, getShippingAddrDetail, postDeleteShippingAddr } from '../../../../api/index'

Page({
  data: {
    form: {
      realName: '',
      mobile: '',
      province: '',
      provinceStr: '',
      city: '',
      cityStr: '',
      area: '',
      areaStr: '',
      detailAddress: '',
      isDefault: false,
      postalCode: ''
    },
    showAddressModal: false
  },
  async delAddress () {
    try {
      const { confirm } = await wx.$pro.showModal({ title: '删除地址', content: '确定要删除该收货地址吗？' })
      if (confirm) {
        wx.$pro.showLoading({ title: '删除中...', mask: true })
        await postDeleteShippingAddr({ id: this.data.form.id })
        wx.showToast({ title: '删除成功！', icon: 'none' })
        wx.$pro.hideLoading()
        wx.$nav.navigateBack()
      }
    } catch (err) {
      wx.$pro.hideLoading()
      wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    }
  },
  async save () {
    try {
      if (!this.data.form.realName) {
        wx.showToast({ title: '请先填写收货人名称！', icon: 'none' })
        return
      }
      if (!this.data.form.mobile) {
        wx.showToast({ title: '请先填写收货人电话！', icon: 'none' })
        return
      }
      if (!this.data.form.province || !this.data.form.detailAddress) {
        wx.showToast({ title: '请先填写收货人地址！', icon: 'none' })
        return
      }
      if (!this.data.form.postalCode) {
        wx.showToast({ title: '请先填写邮政编码', icon: 'none' })
        return
      }
      wx.showLoading({ title: '保存中...', mask: true })
      await saveOrUpdateShippingAddr({ ...this.data.form })
      wx.hideLoading()
      wx.$nav.navigateBack()
    } catch (err) {
      wx.hideLoading()
      wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    }
  },
  changeSwitch ({ detail }) {
    this.setData({ 'form.isDefault': detail })
  },
  changeInput ({ currentTarget: { dataset: { key } }, detail: { value } }) {
    this.setData({ ['form.' + key]: value })
  },
  selectAddress () {
    this.setData({ showAddressModal: true })
  },
  closeModal () {
    this.setData({ showAddressModal: false })
  },
  confirm ({detail}) {
    this.setData({
      'form.province': detail[0].value,
      'form.provinceStr': detail[0].label,
      'form.city': detail[1].value,
      'form.cityStr': detail[1].label,
      'form.area': detail[2].value,
      'form.areaStr': detail[2].label,
      showAddressModal: false
    })
  },
  async getDetail () {
    try {
      const { ResultSet } = await getShippingAddrDetail({ id: this.data.form.id })
      this.data.form = { ...this.data.form, ...ResultSet.userData.detail }
      this.setData({ form: this.data.form })
    } catch (err) {
      console.log(err)
      wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    }
  },
  onLoad ({ id }) {
    if (id) {
      this.setData({ 'form.id': id })
      wx.$pro.setNavigationBarTitle({ title: '编辑地址' })
      this.getDetail()
    } else {
      wx.$pro.setNavigationBarTitle({ title: '新增收货地址' })
    }
  }
})